﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Data.OracleClient;
using System.Text;
using ReflectionStudio.Core.Events;
using WPFExcelReport;
using INTEX.DataBase;
using INTEX.DataBase.Tools;
using INTEX.DataBase;
namespace INTEXTECH
{
    public partial class INTEXTECHData : BaseFunction
    {
        SETINTEXDB SETINTEXDB = new INTEX.DataBase.SETINTEXDB();
        public DataSet SetData = new DataSet();
        public DataSet SetDispo = new DataSet();
        public string ITXMANDANT = "00", ITXUSER = "INTEXMAP", ITXDATE = "20120723";
        public string ITXFART = "                    ", ITXGART = "                    ", ITXBART = "                    ";
        public string ITXFVAR = "  ", ITXGVAR = "  ", ITXBVAR = "1 ";
        public string ITXFCOLOR = "          ", ITXGCOLOR = "          ", ITXBCOLOR = "          ";
        public string ITXFFINISH = "          ", ITXCADID = "1";
        public string CADCHAR = " ", ITXCHAR = " ";
        public INTEXDBC INTEXDBC;
        public INTEXTECHData()
            : base()
        {

        }
        public INTEXTECHData(Dictionary<string, object> DicGlobalPara)
            : base(DicGlobalPara)
        {
        }
        public INTEXTECHData(OracleConnection _OracleDB, IntexReptServBLClient _proxy, string userID = "        ")
            : base(_OracleDB, _proxy, userID)
        {
            INTEXDBC = new INTEXDBC(_OracleDB, _proxy);
        }
        //WASTAMM WTSCHUSS WAFARBEN WTGEW WASTKE WASTGA BSSATZ BHSATZ BSGROE BSFARB BASTGA
        private DataRow GetProcessFlow(string ftype, string MATCHID = "")
        {
            DataRow Return = null;
            DataTable tbFlow = SetDispo.Tables["DATADISPO"];
            #region=======Finish=================
            var query1 = from fl in tbFlow.AsEnumerable()
                         where fl["STAGE"].ToString() == ftype
                         && fl["MATCHID"].ToString() == MATCHID
                         select fl;
            foreach (DataRow row in query1)
            {
                Return = row;
            }
            if (ftype == "F")
            {

            }
            #endregion
            #region=======Greige=================
            if (ftype == "R")
            {
            }
            #endregion
            #region=======Beam=================
            if (ftype == "B")
            {
            }
            #endregion
            #region=======Yarn=================
            if (ftype == "Y")
            {
            }
            #endregion
            return Return;

        }

        public bool HasABPOS(string WAART, string WAVAR)
        {
            bool hasABPOS = false;
            DataSet setReturn = new DataSet();
            DataSet setResult = new DataSet();
            DataTable tableLocal = null;
            OracleCommand or1;
            try
            {
                or1 = OracleDB.CreateCommand();
                or1.CommandText = "SELECT DISTINCT ABPAUFKRS ,ABPAUFLFD FROM ABPOS"
                + " WHERE ABPART = '" + WAART + "' AND ABPVAR='" + WAVAR + "' AND ROWNUM<2"
                + " ORDER BY 1,2";

                setResult = Connect2Proxy(or1);
                if (setResult.Tables.Count > 0)
                {
                    tableLocal = setResult.Tables[0];
                    tableLocal.TableName = "ABPOS";
                    if (tableLocal.Rows.Count > 0)
                        hasABPOS = true;
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return hasABPOS;
        }
        private bool ValidateWASTAMM(DataRow dwInput, ref string msg)
        {
            bool flagValidate = false;
            try
            {
                string WAART = dwInput["WAART"].ToString();
                string WAVAR = dwInput["WAVAR"].ToString();
                if (HasABPOS(WAART, WAVAR))
                {
                    msg = "该品种已经创建生产单，不允许更改技术数据";
                    return flagValidate;
                }
                if (dwInput["WAMANDANT"].ToString().Trim() == "")
                {
                    msg = "MANDANT 为空";
                    return flagValidate;
                }
                if (dwInput["WAART"].ToString().Trim() == "")
                {
                    msg = "WAART 为空";
                    return flagValidate;
                }
                /*if (dwInput["WAART"].ToString().Trim() == "")
                {
                    msg = "WAART 为空";
                    return flagValidate;
                }*/
                if (dwInput["WAVAR"].ToString().Trim() == "")
                {
                    //MsgValidate = "WAVAR 为空";
                    //return flagValidate;
                }

                if (dwInput["WADART"].ToString().Trim() == "")
                {
                    msg = "计划物料不能为空";
                    return flagValidate;
                }
                if (dwInput["WABEZ"].ToString().Trim() == "")
                {
                    msg = "描述不能为空";
                    //return flagValidate;
                }
                if (dwInput["WARWBRT"].ToString().Trim() == "0")
                {
                    msg = "坯布幅宽不能为0";
                    return flagValidate;
                }
                if (dwInput["WAFBRTL"].ToString().Trim() == "0")
                {
                    msg = "成品幅宽不能为0";
                    return flagValidate;
                }
                if (dwInput["WARFDDIK"].ToString().Trim() == "0")
                {
                    msg = "坯布经密>0";
                    return flagValidate;
                }

                if (dwInput["WARFDDIS"].ToString().Trim() == "0")
                {
                    msg = "坯布纬密>0";
                    return flagValidate;
                }
                if (dwInput["WAFFDDIK"].ToString().Trim() == "0")
                {
                    msg = "成品经密>0";
                    return flagValidate;
                }
                if (dwInput["WAFFDDIS"].ToString().Trim() == "0")
                {
                    msg = "成品纬密>0";
                    return flagValidate;
                }
                if (dwInput["WABIND"].ToString().Trim() == "")
                {
                    msg = "请输入插筘法";
                    return flagValidate;
                }
                if (dwInput["WAEINZNR"].ToString().Trim() == "0")
                {
                    msg = "请输入花型";
                    return flagValidate;
                }
                if (dwInput["WALAEAEND"].ToString().Trim() == "0")
                {
                    msg = "整理缩率必须输入";
                    return flagValidate;
                }
                if (dwInput["WATSDSCH"].ToString().Trim() == "0")
                {
                    dwInput["WATSDSCH"] = 7800;
                    msg = "每卷千纬数要仔细计算";
                }
                long WARGRLFD = System.Convert.ToInt64(dwInput["WARGRLFD"].ToString());//坯布克重 398
                long WARWBRT = System.Convert.ToInt64(dwInput["WARWBRT"].ToString());//坯布幅宽 1600
                string WARGRQM = dwInput["WARGRQM"].ToString();//255
                WARGRQM = System.Math.Round(WARGRLFD / (0.001 * WARWBRT), 0).ToString();
                dwInput["WARGRQM"] = WARGRQM;

                long WARSTDLG = System.Convert.ToInt64(dwInput["WARSTDLG"].ToString());//标准匹长 200000
                string WARSTDGW = dwInput["WARSTDGW"].ToString();//标准匹重	7960
                WARSTDGW = System.Math.Round(WARGRLFD * WARSTDLG * 0.001, 0).ToString();
                dwInput["WARSTDGW"] = WARSTDGW;

                long WARFDDIS = System.Convert.ToInt64(dwInput["WARFDDIS"].ToString());//坯布纬密 3200
                string WATSDSCH = dwInput["WATSDSCH"].ToString();//每卷千纬数 2520 252	=四舍五入（10×每卷匹长×100*坯布纬密/2.54/1000）
                WATSDSCH = System.Math.Round(10 * WARSTDLG * 0.001 * 100 * WARFDDIS * 0.01 / 2.54 * 0.001, 0).ToString();
                dwInput["WATSDSCH"] = WATSDSCH;

                long WAFGRLFD = System.Convert.ToInt64(dwInput["WAFGRLFD"].ToString());//克重 398
                long WAFSTDLG = System.Convert.ToInt64(dwInput["WAFSTDLG"].ToString());//标准匹长 200000
                long WAFBRTL = System.Convert.ToInt64(dwInput["WAFBRTL"].ToString());//幅宽 1600
                string WAFGRQM = dwInput["WAFGRQM"].ToString();//255
                WAFGRQM = System.Math.Round(WAFGRLFD / (0.001 * WAFBRTL), 0).ToString();
                dwInput["WAFGRQM"] = WAFGRQM;

                string WAFSTDGW = dwInput["WAFSTDGW"].ToString();//标准匹重	7960
                WAFSTDGW = System.Math.Round(WAFGRLFD * WAFSTDLG * 0.001 * 0.1, 0).ToString();
                dwInput["WAFSTDGW"] = WAFSTDGW;
                flagValidate = true;
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return flagValidate;
        }
        private bool ValidateWTGEW(DataRow dwInput, ref string MsgValidate)
        {
            bool flagValidate = false;
            try
            {

                if (dwInput["WTGMANDANT"].ToString().Trim() == "")
                {
                    MsgValidate = "MANDANT 为空";
                    return flagValidate;
                }
                if (dwInput["WTGART"].ToString().Trim() == "")
                {
                    MsgValidate = "ART 为空";
                    return flagValidate;
                }
                return true;
                if (dwInput["WTGWBBRT"].ToString().Trim() == "0")
                {
                    MsgValidate = "筘幅>0";
                    return flagValidate;
                }
                if (dwInput["WTGWBF"].ToString().Trim() == "0")
                {
                    MsgValidate = "筘齿密度>0";
                    return flagValidate;
                }
                if (dwInput["WTGFDISM"].ToString().Trim() == "0")
                {
                    MsgValidate = "机上纬密>0";
                    return flagValidate;
                }
                if (dwInput["WTGFDLGS"].ToString().Trim() == "0")
                {
                    MsgValidate = "底纬幅宽>0";
                    return flagValidate;
                }

                if (dwInput["WTGFDLFS"].ToString().Trim() == "0")
                {
                    //MsgValidate = "坯布纬密>0";
                    //return flagValidate;
                }
                if (dwInput["WTGSFLG"].ToString().Trim() == "")
                {
                    MsgValidate = "请输入纬向排花>0";
                    return flagValidate;
                }
                if (dwInput["WTGSMTRS"].ToString().Trim() == "0")
                {
                    //MsgValidate = "成品纬密>0";
                    //return flagValidate;
                }
                flagValidate = true;
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return flagValidate;
        }
        public void ReSet(DataSet setData, DataSet setDispo)
        {
            try
            {
                this.SetData = setData;
                this.SetDispo = setDispo;
                UserID = "INTEX   ";
                if (!this.SetData.Tables.Contains("INTEXMAP"))
                    return;
                if (this.SetData.Tables["INTEXMAP"].Rows.Count == 0)
                    return;
                DataRow dwIntex = this.SetData.Tables["INTEXMAP"].Rows[0];
                ITXMANDANT = dwIntex["ITXMANDANT"].ToString();
                ITXCADID = dwIntex["ITXCADID"].ToString();
                ITXFART = dwIntex["ITXFART"].ToString();
                ITXFVAR = dwIntex["ITXFVAR"].ToString();
                ITXFCOLOR = dwIntex["ITXFCOLOR"].ToString();
                ITXFFINISH = dwIntex["ITXFFINISH"].ToString();
                ITXGART = dwIntex["ITXGART"].ToString();
                ITXGVAR = dwIntex["ITXGVAR"].ToString();
                ITXGCOLOR = dwIntex["ITXGCOLOR"].ToString();
                ITXBART = dwIntex["ITXBART"].ToString();
                ITXBVAR = dwIntex["ITXBVAR"].ToString();
                ITXBCOLOR = dwIntex["ITXBCOLOR"].ToString();
                ITXUSER = dwIntex["ITXUSER"].ToString();
                ITXDATE = dwIntex["ITXDATE"].ToString();
                CADCHAR = dwIntex["CADCHAR"].ToString();
                ITXCHAR = dwIntex["ITXCHAR"].ToString();
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        public void Delete()
        {
            try
            {
                //DeleteWASTAMM,DeleteWTSCHUSS,DeleteWAFARBEN,DeleteSDDAT,DeleteWASTPASS,DeleteWTPASS
                //DeleteWTGEW,DeleteWASTKE,DeleteWASTGA,DeleteBSSATZ,DeleteBHSATZ,DeleteBSGROE
                //DeleteBSFARB,DeleteBASTGA
                INTEXDBC.DeleteWASTAMM(ITXMANDANT, ITXFART, ITXFVAR);
                INTEXDBC.DeleteWTSCHUSS(ITXMANDANT, ITXCADID);
                INTEXDBC.DeleteWAFARBEN(ITXMANDANT, ITXGART, ITXGVAR, ITXGCOLOR);
                INTEXDBC.DeleteWTEINZ(ITXMANDANT, ITXCADID);
                INTEXDBC.DeleteWASTPASS(ITXMANDANT, ITXFART, ITXFVAR);
                INTEXDBC.DeleteWTPASS(ITXMANDANT, ITXCADID);
                INTEXDBC.DeleteWTGEW(ITXMANDANT, ITXGART, ITXGVAR);
                INTEXDBC.DeleteWASTKE(ITXMANDANT, ITXGART, ITXGVAR, ITXGCOLOR);
                INTEXDBC.DeleteWASTGA(ITXMANDANT, ITXGART, ITXGVAR, ITXGCOLOR);
                INTEXDBC.DeleteBSSATZ(ITXMANDANT, ITXBART);
                INTEXDBC.DeleteWTSAER(ITXMANDANT, ITXCADID);
                INTEXDBC.DeleteBHSATZ(ITXMANDANT, ITXBART, ITXBVAR);
                INTEXDBC.DeleteBSGROE(ITXMANDANT, ITXBART);
                INTEXDBC.DeleteBSFARB(ITXMANDANT, ITXBART, ITXBVAR, ITXBCOLOR);
                INTEXDBC.DeleteBASTGA(ITXMANDANT, ITXBART, ITXBVAR, ITXBCOLOR);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        public bool Execute(bool IsUpdateVa = false)
        {
            DataSet set4Return = new DataSet();
            DataSet setResult = new DataSet();
            bool flagValidate = false;
            string MsgValidate = "";
            DataRow dwWTGEW = null, dwWASTAMM = null, dwWASTPASS = null, dwVAART = null, dwVAFARBEN = null, dwWDECK = null, dwWTPASS = null;
            DataRow dwDispo = SetDispo.Tables["DATADISPO"].Rows[0];
            string art, var, color, finish = "";
            string stage;
            string matchid = "";
            string expression = "";
            DataRow[] rows;
            DataTable tbAPZUORD = new DataTable();
            DataRow dwAPZUORD = null;
            DataTable tbWASTAMM, tbWTGEW, tbWASTPASS, tbVAART, tbVAFARBEN = null, tbWDECK = null, tbWTPASS, tbWAFARBEN;
            try
            {
                INTEXDBC = new INTEXDBC(OracleDB, proxy);

                #region==========VALIDATE========================

                tbWASTAMM = SetData.Tables["WASTAMM"];
                tbWTGEW = SetData.Tables["WTGEW"];
                tbWASTPASS = SetData.Tables["WASTPASS"];
                tbWTPASS = SetData.Tables["WTPASS"];
                tbWAFARBEN = SetData.Tables["WAFARBEN"];


                if (tbWASTAMM.Rows.Count == 0)
                {
                    EventDispatcher.Instance.RaiseStatus("WASTAMM没有数据", StatusEventType.StopProgress);
                    return false;
                }
                dwWASTAMM = tbWASTAMM.Rows[0];
                if (tbWTGEW.Rows.Count == 0)
                {
                    EventDispatcher.Instance.RaiseStatus("WTGEW没有数据", StatusEventType.StopProgress);
                    return false;
                }
                dwWTGEW = tbWTGEW.Rows[0];

                if (tbWASTPASS.Rows.Count == 0)
                {
                    EventDispatcher.Instance.RaiseStatus("WASTPASS没有数据", StatusEventType.StopProgress);
                    return false;
                }
                dwWASTPASS = tbWASTPASS.Rows[0];
                if (tbWTPASS != null)
                {
                    if (tbWTPASS.Rows.Count > 0)
                    {
                        dwWTPASS = tbWTPASS.Rows[0];
                    }
                }
                if (tbWAFARBEN.Rows.Count == 0)
                {
                    EventDispatcher.Instance.RaiseStatus("WAFARBEN没有数据", StatusEventType.StopProgress);
                    return false;
                }

                flagValidate = ValidateWASTAMM(dwWASTAMM, ref MsgValidate);
                if (!flagValidate)
                {
                    EventDispatcher.Instance.RaiseStatus("ValidateWASTAMM未通过检查:" + MsgValidate, StatusEventType.StopProgress);
                    return false;
                }
                dwWTGEW = SetData.Tables["WTGEW"].Rows[0];
                flagValidate = ValidateWTGEW(dwWTGEW, ref MsgValidate);
                if (!flagValidate)
                {
                    return false;
                }
                tbAPZUORD = SetData.Tables["APZUORD"];//DST屏蔽

                #endregion

                #region=====FABRIC========

                INTEXDBC.SaveWASTAMM(dwWASTAMM, dwDispo);
                stage = "F";
                color = "";
                art = dwWASTAMM["WAART"].ToString();
                var = dwWASTAMM["WAVAR"].ToString();
                foreach (DataRow dwWS07 in tbWAFARBEN.Rows)
                {
                    string WAFZUART = dwWS07["WAFZUART"].ToString();
                    if (WAFZUART == "C")
                    {
                        color = dwWS07["WAFZUSATZ"].ToString();
                    }
                    if (WAFZUART == "B")
                    {
                        finish = dwWS07["WAFZUSATZ"].ToString();
                    }
                }
                #region===GetVA07/VA10/WDECK==========
                DataRow row = SETINTEXDB.VAART.NewRow();
                row["VAAMANDANT"] = ITXMANDANT;
                row["VAAART"] = Left(art + "                 ", 24);
                setResult = INTEXDBC.QueryVAART(row, 2);
                SetData.Merge(setResult);
                tbVAART = SetData.Tables["VAART"];
                if (tbVAART.Rows.Count > 0)
                {
                    dwVAART = tbVAART.Rows[0];
                    row = SETINTEXDB.VAFARBEN.NewRow();
                    row["VAFMANDANT"] = dwVAART["VAAMANDANT"].ToString();
                    row["VAFART"] = Left(art + "              ", 24);
                    row["VAFKOLL"] = dwVAART["VAAKOLL"].ToString();
                    row["VAFFARBE"] = Left(color + "                    ", 10);

                    setResult = INTEXDBC.QueryVAFARBEN(row, 2);
                    SetData.Merge(setResult);
                    tbVAFARBEN = SetData.Tables["VAFARBEN"];
                    foreach (DataRow rowx in tbVAFARBEN.Rows)
                    {
                        //rowx["VAFBESCHICH"] = Left(finish + "                    ", 10);
                        dwVAFARBEN = rowx;
                    }
                    row = SETINTEXDB.WDECK.NewRow();
                    row["WDMANDANT"] = ITXMANDANT;
                    row["WDART"] = Left(art + "               ", 20);
                    row["WDFARBE"] = Left(color + "               ", 10);
                    setResult = INTEXDBC.QueryWDECK(row, 2);
                    SetData.Merge(setResult);
                    tbWDECK = SetData.Tables["WDECK"];
                    foreach (DataRow rowx in tbWDECK.Rows)
                    {
                        //   rowx["WDBESCHICH"] = Left(finish + "                    ", 10);
                        dwWDECK = rowx;
                    }
                }
                #endregion
                EventDispatcher.Instance.RaiseStatus("保存WASTAMM:" + art + var + color + finish, StatusEventType.StartProgress);
                matchid = finish;
                dwAPZUORD = null;
                expression = "APZART='" + art + "' AND APZVAR='" + var + "' AND APZSTUFE='" + stage + "'";
                rows = tbAPZUORD.Select(expression);
                foreach (DataRow dwTemp in rows)
                {
                    //INTEXDBC.SaveAPZUORD(dwTemp, dwDispo);
                }
                stage = "R";
                art = dwWASTAMM["WAVWART"].ToString();
                var = dwWASTAMM["WAVWVAR"].ToString();
                expression = "APZART='" + art + "' AND APZVAR='" + var + "' AND APZSTUFE='" + stage + "'";
                rows = tbAPZUORD.Select(expression);
                foreach (DataRow dwTemp in rows)
                {
                    INTEXDBC.SaveAPZUORD(dwTemp, dwDispo);
                }
                EventDispatcher.Instance.RaiseStatus("保存InsertWABIND:" + art + var + color + finish, StatusEventType.RunningProgress);

                foreach (DataRow dwSDDAT in SetData.Tables["SDDAT"].Rows)
                {
                    INTEXDBC.SaveSDDAT(dwSDDAT);
                }
                EventDispatcher.Instance.RaiseStatus("保存WTEINZ:" + art + var + color + finish, StatusEventType.RunningProgress);
                foreach (DataRow dwWTEINZ in SetData.Tables["WTEINZ"].Rows)
                {
                    INTEXDBC.SaveWTEINZ(dwWTEINZ);
                }
                EventDispatcher.Instance.RaiseStatus("保存WTGEW:" + art + var + color + finish, StatusEventType.RunningProgress);
                INTEXDBC.SaveWTGEW(dwWTGEW);
                EventDispatcher.Instance.RaiseStatus("保存WASTPASS:" + art + var + color + finish, StatusEventType.RunningProgress);
                INTEXDBC.SaveWASTPASS(dwWASTPASS);
                EventDispatcher.Instance.RaiseStatus("保存WTSCHUSS:" + art + var + color + finish, StatusEventType.RunningProgress);
                foreach (DataRow dwTemp in SetData.Tables["WTSCHUSS"].Rows)
                {
                    INTEXDBC.SaveWTSCHUSS(dwTemp);
                }

                EventDispatcher.Instance.RaiseStatus("开始保存WTPASS:" + art + var + color + finish, StatusEventType.RunningProgress);
                INTEXDBC.SaveWTPASS(dwWTPASS);

                EventDispatcher.Instance.RaiseStatus("开始保存WTSAER:" + art + var + color + finish, StatusEventType.RunningProgress);
                foreach (DataRow dwTemp in SetData.Tables["WTSAER"].Rows)
                {
                    INTEXDBC.SaveWTSAER(dwTemp);
                }
                EventDispatcher.Instance.RaiseStatus("开始保存WAFARBEN:" + art + var + color + finish, StatusEventType.RunningProgress);
                if (tbWAFARBEN != null)
                {
                    foreach (DataRow dwTemp in tbWAFARBEN.Rows)
                    {
                        //DST将色号替代后整理方式
                        if (dwTemp["WAFZUART"].ToString() == "B")
                        {
                            if (dwVAFARBEN != null)
                            {
                                dwTemp["WAFZUSATZ"] = dwVAFARBEN["VAFBESCHICH"];
                            }
                        }
                        INTEXDBC.SaveWAFARBEN(dwTemp);
                    }
                }
                EventDispatcher.Instance.RaiseStatus("开始保存WASTKE:" + art + var + color + finish, StatusEventType.RunningProgress);
                foreach (DataRow dwTemp in SetData.Tables["WASTKE"].Rows)
                {
                    INTEXDBC.SaveWASTKE(dwTemp, dwDispo);
                }
                EventDispatcher.Instance.RaiseStatus("开始保存WASTGA:" + art + var + color + finish, StatusEventType.RunningProgress);
                if (SetData.Tables["WASTGA"].Rows.Count > 0)
                {
                    DataRow dwData = SetData.Tables["WASTGA"].Rows[0];
                    setResult = INTEXDBC.QueryWASTGA(dwData);
                    if (setResult.Tables.Count > 0)
                    {
                        if (setResult.Tables[0].Rows.Count > 0)
                        {
                            INTEXDBC.DeleteWASTGA(ITXMANDANT, ITXGART, ITXGVAR, ITXGCOLOR);
                        }
                    }
                }

                foreach (DataRow dwTemp in SetData.Tables["WASTGA"].Rows)
                {
                    INTEXDBC.SaveWASTGA(dwTemp, dwDispo);
                }

                if (IsUpdateVa)
                {
                    if (dwVAFARBEN != null)
                    {
                        EventDispatcher.Instance.RaiseStatus("开始保存VA0710:" + art + var + color + finish, StatusEventType.RunningProgress);
                        foreach (DataRow rowx in tbVAART.Rows)
                        {
                            INTEXDBC.UpdateVAART(rowx);
                        }
                        foreach (DataRow rowx in tbVAFARBEN.Rows)
                        {
                            INTEXDBC.UpdateVAFARBEN(rowx);
                        }
                        EventDispatcher.Instance.RaiseStatus("开始保存WDECK:" + art + var + color + finish, StatusEventType.RunningProgress);
                        foreach (DataRow rowx in tbWDECK.Rows)
                        {
                            INTEXDBC.UpdateWDECK(rowx);
                        }
                    }
                }
                #endregion

                #region=====BEAM========
                EventDispatcher.Instance.RaiseStatus("开始保存BSSATZ:" + art + var + color + finish, StatusEventType.RunningProgress);
                foreach (DataRow dwTemp in SetData.Tables["BSSATZ"].Rows)
                {
                    INTEXDBC.SaveBSSATZ(dwTemp, dwDispo);
                }
                EventDispatcher.Instance.RaiseStatus("开始保存BHSATZ:" + art + var + color + finish, StatusEventType.RunningProgress);
                foreach (DataRow dwTemp in SetData.Tables["BHSATZ"].Rows)
                {
                    INTEXDBC.SaveBHSATZ(dwTemp, dwDispo);
                    stage = "B";
                    // BHKART BHKVAR
                    art = dwTemp["BHKART"].ToString();
                    var = dwTemp["BHKVAR"].ToString();
                    expression = "APZART='" + art + "' AND APZVAR='" + var + "' AND APZSTUFE='" + stage + "'";
                    rows = tbAPZUORD.Select(expression);
                    foreach (DataRow dwFlow in rows)
                    {
                        INTEXDBC.SaveAPZUORD(dwFlow, dwDispo);
                    }
                }
                EventDispatcher.Instance.RaiseStatus("开始保存BSFARB:" + art + var + color + finish, StatusEventType.RunningProgress);
                foreach (DataRow dwTemp in SetData.Tables["BSFARB"].Rows)
                {
                    INTEXDBC.SaveBSFARB(dwTemp, dwDispo);
                }
                EventDispatcher.Instance.RaiseStatus("开始保存BSGROE:" + art + var + color + finish, StatusEventType.RunningProgress);
                foreach (DataRow dwTemp in SetData.Tables["BSGROE"].Rows)
                {
                    INTEXDBC.SaveBSGROE(dwTemp, dwDispo);
                }
                EventDispatcher.Instance.RaiseStatus("开始保存BASTGA:" + art + var + color + finish, StatusEventType.RunningProgress);
                if (SetData.Tables["BASTGA"].Rows.Count > 0)
                {
                    DataRow dwData = SetData.Tables["BASTGA"].Rows[0];
                    setResult = INTEXDBC.QueryBASTGA(dwData);
                    if (setResult.Tables.Count > 0)
                    {
                        if (setResult.Tables[0].Rows.Count > 0)
                        {
                            INTEXDBC.DeleteBASTGA(ITXMANDANT, ITXBART, ITXBVAR, ITXBCOLOR);
                            INTEXDBC.DeleteBASTGAD(ITXMANDANT, ITXBART, ITXBVAR, ITXBCOLOR);
                        }
                    }
                }
                EventDispatcher.Instance.RaiseStatus("开始保存BASTGA:" + art + var + color + finish, StatusEventType.RunningProgress);
                foreach (DataRow dwTemp in SetData.Tables["BASTGA"].Rows)
                {
                    INTEXDBC.SaveBASTGA(dwTemp, dwDispo);
                }
                if (SetData.Tables.Contains("BASTGAD"))
                {
                    foreach (DataRow dwTemp in SetData.Tables["BASTGAD"].Rows)
                    {
                        INTEXDBC.SaveBASTGAD(dwTemp, dwDispo);
                    }
                }
                #endregion
                #region=====YARN========
                EventDispatcher.Instance.RaiseStatus("开始保存GSCOLOR:" + art + var + color + finish, StatusEventType.RunningProgress);
                foreach (DataRow dwTemp in SetData.Tables["GSCOLOR"].Rows)
                {
                    INTEXDBC.SaveGSCOLOR(dwTemp);
                    stage = "Y";
                    // GSCART GSCVAR
                    art = dwTemp["GSCART"].ToString();
                    var = dwTemp["GSCVAR"].ToString();
                    expression = "APZART='" + art + "' AND APZVAR='" + var + "' AND APZSTUFE='" + stage + "'";
                    rows = tbAPZUORD.Select(expression);
                    foreach (DataRow dwFlow in rows)
                    {
                        //INTEXDBC.SaveAPZUORD(dwFlow, dwDispo);
                    }
                }
                #endregion
                EventDispatcher.Instance.RaiseStatus("保存完成:" + art + var + color + finish, StatusEventType.StopProgress);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            finally
            {
                EventDispatcher.Instance.RaiseStatus("保存完成", StatusEventType.StopProgress);
            }
            return true;
        }
        public bool Execute(int option,bool IsUpdateVa = false)
        {
            DataSet set4Return = new DataSet();
            DataSet setResult = new DataSet();
            bool flagValidate = false;
            string MsgValidate = "";
            DataRow dwWTGEW = null, dwWASTAMM = null, dwWASTPASS = null, dwVAART = null, dwVAFARBEN = null, dwWDECK = null, dwWTPASS = null;
            DataRow dwDispo = SetDispo.Tables["DATADISPO"].Rows[0];
            string art, var, color, finish = "";
            string stage;
            string matchid = "";
            string expression = "";
            DataRow[] rows;
            DataTable tbAPZUORD = new DataTable();
            DataRow dwAPZUORD = null;
            DataTable tbWASTAMM, tbWTGEW, tbWASTPASS, tbVAART, tbVAFARBEN = null, tbWDECK = null, tbWTPASS, tbWAFARBEN;
            try
            {
                INTEXDBC = new INTEXDBC(OracleDB, proxy);

                #region==========VALIDATE========================

                tbWASTAMM = SetData.Tables["WASTAMM"];
                tbWTGEW = SetData.Tables["WTGEW"];
                tbWASTPASS = SetData.Tables["WASTPASS"];
                tbWTPASS = SetData.Tables["WTPASS"];
                tbWAFARBEN = SetData.Tables["WAFARBEN"];
                if (tbWASTAMM == null)
                    return false;

                if (tbWASTAMM.Rows.Count == 0)
                {
                    EventDispatcher.Instance.RaiseStatus("WASTAMM没有数据", StatusEventType.StopProgress);
                    return false;
                }
                dwWASTAMM = tbWASTAMM.Rows[0];
                if (tbWTGEW.Rows.Count == 0)
                {
                    EventDispatcher.Instance.RaiseStatus("WTGEW没有数据", StatusEventType.StopProgress);
                    return false;
                }
                dwWTGEW = tbWTGEW.Rows[0];

                if (tbWASTPASS.Rows.Count == 0)
                {
                    EventDispatcher.Instance.RaiseStatus("WASTPASS没有数据", StatusEventType.StopProgress);
                    return false;
                }
                dwWASTPASS = tbWASTPASS.Rows[0];
                if (tbWTPASS != null)
                {
                    if (tbWTPASS.Rows.Count > 0)
                    {
                        dwWTPASS = tbWTPASS.Rows[0];
                    }
                }
                if (tbWAFARBEN.Rows.Count == 0)
                {
                    EventDispatcher.Instance.RaiseStatus("WAFARBEN没有数据", StatusEventType.StopProgress);
                    return false;
                }

                flagValidate = ValidateWASTAMM(dwWASTAMM, ref MsgValidate);
                if (!flagValidate)
                {
                    EventDispatcher.Instance.RaiseStatus("ValidateWASTAMM未通过检查:" + MsgValidate, StatusEventType.StopProgress);
                    return false;
                }
                dwWTGEW = SetData.Tables["WTGEW"].Rows[0];
                flagValidate = ValidateWTGEW(dwWTGEW, ref MsgValidate);
                if (!flagValidate)
                {
                    return false;
                }
                tbAPZUORD = SetData.Tables["APZUORD"];//DST屏蔽

                #endregion

                #region=====FABRIC========

                INTEXDBC.SaveWASTAMM(dwWASTAMM, dwDispo);
                stage = "F";
                color = "";
                art = dwWASTAMM["WAART"].ToString();
                var = dwWASTAMM["WAVAR"].ToString();
                foreach (DataRow dwWS07 in tbWAFARBEN.Rows)
                {
                    string WAFZUART = dwWS07["WAFZUART"].ToString();
                    if (WAFZUART == "C")
                    {
                        color = dwWS07["WAFZUSATZ"].ToString();
                    }
                    if (WAFZUART == "B")
                    {
                        finish = dwWS07["WAFZUSATZ"].ToString();
                    }
                }
                #region===GetVA07/VA10/WDECK/VKPOS==========
                DataRow row = SETINTEXDB.VAART.NewRow();
                row["VAAMANDANT"] = ITXMANDANT;
                row["VAAART"] = Left(art + "                 ", 24);
                setResult = INTEXDBC.QueryVAART(row, 2);

                tbVAART = setResult.Tables["VAART"];
                if (tbVAART.Rows.Count > 0)
                {
                    foreach (DataRow rx in tbVAART.Rows)
                    {
                        rx["VAADART"] = Left(art + "               ", 20);
                        rx["VAADKUNDNR"] = "00000000";
                        rx["VAALO"] = "  ";
                        rx["VAABESTANDF"] = "J";
                    }
                    dwVAART = tbVAART.Rows[0];
                    row = SETINTEXDB.VAFARBEN.NewRow();
                    row["VAFMANDANT"] = dwVAART["VAAMANDANT"].ToString();
                    row["VAFART"] = Left(art + "              ", 24);
                    row["VAFKOLL"] = dwVAART["VAAKOLL"].ToString();
                    row["VAFFARBE"] = Left(color + "                    ", 10);

                    setResult = INTEXDBC.QueryVAFARBEN(row, 2);

                    tbVAFARBEN = setResult.Tables["VAFARBEN"];
                    foreach (DataRow rowx in tbVAFARBEN.Rows)
                    {
                        dwVAFARBEN = rowx;
                    }
                    row = SETINTEXDB.WDECK.NewRow();
                    row["WDMANDANT"] = ITXMANDANT;
                    row["WDART"] = Left(art + "               ", 20);
                    row["WDFARBE"] = Left(color + "               ", 10);
                    setResult = INTEXDBC.QueryWDECK(row, 2);
                    tbWDECK = setResult.Tables["WDECK"];
                    foreach (DataRow rowx in tbWDECK.Rows)
                    {
                        dwWDECK = rowx;
                    }
                }
                #endregion
                EventDispatcher.Instance.RaiseStatus("保存WASTAMM:" + art + var + color + finish, StatusEventType.StartProgress);
                matchid = finish;
                dwAPZUORD = null;
                expression = "APZART='" + art + "' AND APZVAR='" + var + "' AND APZSTUFE='" + stage + "'";
                rows = tbAPZUORD.Select(expression);
                foreach (DataRow dwTemp in rows)
                {
                    //INTEXDBC.SaveAPZUORD(dwTemp, dwDispo);
                }
                stage = "R";
                art = dwWASTAMM["WAVWART"].ToString();
                var = dwWASTAMM["WAVWVAR"].ToString();
                expression = "APZART='" + art + "' AND APZVAR='" + var + "' AND APZSTUFE='" + stage + "'";
                rows = tbAPZUORD.Select(expression);
                foreach (DataRow dwTemp in rows)
                {
                    INTEXDBC.SaveAPZUORD(dwTemp, dwDispo);
                }
                EventDispatcher.Instance.RaiseStatus("保存InsertWABIND:" + art + var + color + finish, StatusEventType.RunningProgress);

                foreach (DataRow dwSDDAT in SetData.Tables["SDDAT"].Rows)
                {
                    INTEXDBC.SaveSDDAT(dwSDDAT);
                }
                EventDispatcher.Instance.RaiseStatus("保存WTEINZ:" + art + var + color + finish, StatusEventType.RunningProgress);
                foreach (DataRow dwWTEINZ in SetData.Tables["WTEINZ"].Rows)
                {
                    INTEXDBC.SaveWTEINZ(dwWTEINZ);
                }
                EventDispatcher.Instance.RaiseStatus("保存WTGEW:" + art + var + color + finish, StatusEventType.RunningProgress);
                INTEXDBC.SaveWTGEW(dwWTGEW);
                EventDispatcher.Instance.RaiseStatus("保存WASTPASS:" + art + var + color + finish, StatusEventType.RunningProgress);
                INTEXDBC.SaveWASTPASS(dwWASTPASS);
                EventDispatcher.Instance.RaiseStatus("保存WTSCHUSS:" + art + var + color + finish, StatusEventType.RunningProgress);
                foreach (DataRow dwTemp in SetData.Tables["WTSCHUSS"].Rows)
                {
                    INTEXDBC.SaveWTSCHUSS(dwTemp);
                }

                EventDispatcher.Instance.RaiseStatus("开始保存WTPASS:" + art + var + color + finish, StatusEventType.RunningProgress);
                INTEXDBC.SaveWTPASS(dwWTPASS);

                EventDispatcher.Instance.RaiseStatus("开始保存WTSAER:" + art + var + color + finish, StatusEventType.RunningProgress);
                foreach (DataRow dwTemp in SetData.Tables["WTSAER"].Rows)
                {
                    INTEXDBC.SaveWTSAER(dwTemp);
                }
                EventDispatcher.Instance.RaiseStatus("开始保存WAFARBEN:" + art + var + color + finish, StatusEventType.RunningProgress);
                if (tbWAFARBEN != null)
                {
                    foreach (DataRow dwTemp in tbWAFARBEN.Rows)
                    {
                        //DST将色号替代后整理方式
                        if (dwTemp["WAFZUART"].ToString() == "B")
                        {
                            if (dwVAFARBEN != null)
                            {
                                dwTemp["WAFZUSATZ"] = dwVAFARBEN["VAFBESCHICH"];
                            }
                        }
                        INTEXDBC.SaveWAFARBEN(dwTemp);
                    }
                }
                EventDispatcher.Instance.RaiseStatus("开始保存WASTKE:" + art + var + color + finish, StatusEventType.RunningProgress);
                foreach (DataRow dwTemp in SetData.Tables["WASTKE"].Rows)
                {
                    INTEXDBC.SaveWASTKE(dwTemp, dwDispo);
                }
                EventDispatcher.Instance.RaiseStatus("开始保存WASTGA:" + art + var + color + finish, StatusEventType.RunningProgress);
                if (SetData.Tables["WASTGA"].Rows.Count > 0)
                {
                    DataRow dwData = SetData.Tables["WASTGA"].Rows[0];
                    setResult = INTEXDBC.QueryWASTGA(dwData);
                    if (setResult.Tables.Count > 0)
                    {
                        if (setResult.Tables[0].Rows.Count > 0)
                        {
                            INTEXDBC.DeleteWASTGA(ITXMANDANT, ITXGART, ITXGVAR, ITXGCOLOR);
                        }
                    }
                }

                foreach (DataRow dwTemp in SetData.Tables["WASTGA"].Rows)
                {
                    INTEXDBC.SaveWASTGA(dwTemp, dwDispo);
                }

                if (IsUpdateVa)
                {
                    if (SetData.Tables.Contains("VAART"))
                    {
                        EventDispatcher.Instance.RaiseStatus("开始保存VA0710:" + art + var + color + finish, StatusEventType.RunningProgress);
                        foreach (DataRow rowx in SetData.Tables["VAART"].Rows)
                        {
                            INTEXDBC.UpdateVAART(rowx);
                        }
                    }
                    if (SetData.Tables.Contains("VAFARBEN"))
                    {
                        foreach (DataRow rowx in SetData.Tables["VAFARBEN"].Rows)
                        {
                            INTEXDBC.UpdateVAFARBEN(rowx);
                        }
                    }
                    EventDispatcher.Instance.RaiseStatus("开始保存WDECK:" + art + var + color + finish, StatusEventType.RunningProgress);
                    if (SetData.Tables.Contains("WDECK"))
                    {
                        foreach (DataRow rowx in SetData.Tables["WDECK"].Rows)
                        {
                            INTEXDBC.SaveWDECK(rowx);
                        }
                    }
                    if (SetData.Tables.Contains("VKPOS"))
                    {
                        foreach (DataRow rowx in SetData.Tables["VKPOS"].Rows)
                        {
                            INTEXDBC.SaveVKPOS(rowx,"",2);
                        }
                    }
                }
                #endregion

                #region=====BEAM========
                EventDispatcher.Instance.RaiseStatus("开始保存BSSATZ:" + art + var + color + finish, StatusEventType.RunningProgress);
                foreach (DataRow dwTemp in SetData.Tables["BSSATZ"].Rows)
                {
                    INTEXDBC.SaveBSSATZ(dwTemp, dwDispo);
                }
                EventDispatcher.Instance.RaiseStatus("开始保存BHSATZ:" + art + var + color + finish, StatusEventType.RunningProgress);
                foreach (DataRow dwTemp in SetData.Tables["BHSATZ"].Rows)
                {
                    INTEXDBC.SaveBHSATZ(dwTemp, dwDispo);
                    stage = "B";
                    // BHKART BHKVAR
                    art = dwTemp["BHKART"].ToString();
                    var = dwTemp["BHKVAR"].ToString();
                    expression = "APZART='" + art + "' AND APZVAR='" + var + "' AND APZSTUFE='" + stage + "'";
                    rows = tbAPZUORD.Select(expression);
                    foreach (DataRow dwFlow in rows)
                    {
                        INTEXDBC.SaveAPZUORD(dwFlow, dwDispo);
                    }
                }
                EventDispatcher.Instance.RaiseStatus("开始保存BSFARB:" + art + var + color + finish, StatusEventType.RunningProgress);
                foreach (DataRow dwTemp in SetData.Tables["BSFARB"].Rows)
                {
                    INTEXDBC.SaveBSFARB(dwTemp, dwDispo);
                }
                EventDispatcher.Instance.RaiseStatus("开始保存BSGROE:" + art + var + color + finish, StatusEventType.RunningProgress);
                foreach (DataRow dwTemp in SetData.Tables["BSGROE"].Rows)
                {
                    INTEXDBC.SaveBSGROE(dwTemp, dwDispo);
                }
                EventDispatcher.Instance.RaiseStatus("开始保存BASTGA:" + art + var + color + finish, StatusEventType.RunningProgress);
                if (SetData.Tables["BASTGA"].Rows.Count > 0)
                {
                    DataRow dwData = SetData.Tables["BASTGA"].Rows[0];
                    setResult = INTEXDBC.QueryBASTGA(dwData);
                    if (setResult.Tables.Count > 0)
                    {
                        if (setResult.Tables[0].Rows.Count > 0)
                        {
                            INTEXDBC.DeleteBASTGA(ITXMANDANT, ITXBART, ITXBVAR, ITXBCOLOR);
                            INTEXDBC.DeleteBASTGAD(ITXMANDANT, ITXBART, ITXBVAR, ITXBCOLOR);
                        }
                    }
                }
                EventDispatcher.Instance.RaiseStatus("开始保存BASTGA:" + art + var + color + finish, StatusEventType.RunningProgress);
                foreach (DataRow dwTemp in SetData.Tables["BASTGA"].Rows)
                {
                    INTEXDBC.SaveBASTGA(dwTemp, dwDispo);
                }
                if (SetData.Tables.Contains("BASTGAD"))
                {
                    foreach (DataRow dwTemp in SetData.Tables["BASTGAD"].Rows)
                    {
                        INTEXDBC.SaveBASTGAD(dwTemp, dwDispo);
                    }
                }
                #endregion
                #region=====YARN========
                EventDispatcher.Instance.RaiseStatus("开始保存GSCOLOR:" + art + var + color + finish, StatusEventType.RunningProgress);
                if (SetData.Tables.Contains("GSCOLOR"))
                {
                    foreach (DataRow dwTemp in SetData.Tables["GSCOLOR"].Rows)
                    {
                        INTEXDBC.SaveGSCOLOR(dwTemp);
                        stage = "Y";
                        // GSCART GSCVAR
                        art = dwTemp["GSCART"].ToString();
                        var = dwTemp["GSCVAR"].ToString();
                        expression = "APZART='" + art + "' AND APZVAR='" + var + "' AND APZSTUFE='" + stage + "'";
                        rows = tbAPZUORD.Select(expression);
                        foreach (DataRow dwFlow in rows)
                        {
                            //INTEXDBC.SaveAPZUORD(dwFlow, dwDispo);
                        }
                    }
                }
                #endregion
                EventDispatcher.Instance.RaiseStatus("保存完成:" + art + var + color + finish, StatusEventType.StopProgress);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            finally
            {
                EventDispatcher.Instance.RaiseStatus("保存完成", StatusEventType.StopProgress);
            }
            return true;
        }
    }
}